package com.srs.backend.serviceImpl;

import com.srs.backend.mapper.FoodMapper;
import com.srs.backend.mapper.FoodMaterialMapper;
import com.srs.backend.mapper.FoodStyleMapper;
import com.srs.backend.mapper.MaterialMapper;
import com.srs.backend.pojo.Material;
import com.srs.backend.service.MaterialService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Resource
    private FoodMapper foodMapper;
    @Resource
    private FoodMaterialMapper foodMaterialMapper;
    @Resource
    private FoodStyleMapper foodStyleMapper;
    @Resource
    private MaterialMapper materialMapper;

    @Override
    public Result add(Map<String, String> data) {
        String name = data.get("name");
        String numStr = data.get("num");
        String gpStr = data.get("guarantee_period");
        String createTimeStr = data.get("create_time");
        Date createTime = null;
        Integer num = null;
        Integer gp = null;
        try {
            num = Integer.parseInt(numStr);
            gp = Integer.parseInt(gpStr);
        } catch (NumberFormatException e) {
            return Result.error("数量或保质期 数据格式有误");
        }
        if (name == null || name.length() == 0) {
            return Result.error("原材料名称不能为空");
        }
        if (name.length() > 20) {
            return Result.error("原材料名称过长");
        }
        if (num < 0 || num > 100000) {
            return Result.error("原材料数量范围有误");
        }
        if (gp < 0 || gp > 100000) {
            return Result.error("原材料保质期范围有误");
        }
        if (createTimeStr == null) {
            return Result.error("生产日期不能为空");
        }
        try {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            createTime = ft.parse(createTimeStr);
        } catch (ParseException e) {
            return Result.error("生产日期格式有误");
        }
        materialMapper.insert(new Material(null, name, num, createTime, gp, null));
        return Result.ok();
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Material curMaterial = materialMapper.selectById(id);
        if (curMaterial == null) {
            return Result.error("当前原材料不存在或已被删除");
        }
        materialMapper.deleteById(curMaterial);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Material curMaterial = materialMapper.selectById(id);
        if (curMaterial == null) {
            return Result.error("当前原材料不存在或已被删除");
        }

        String name = data.get("name");
        String numStr = data.get("num");
        String gpStr = data.get("guarantee_period");
        String createTimeStr = data.get("create_time");
        Date createTime = null;

        Integer num = null;
        Integer gp = null;
        try {
            num = Integer.parseInt(numStr);
            gp = Integer.parseInt(gpStr);
        } catch (NumberFormatException e) {
            return Result.error("数量或保质期 数据格式有误");
        }
        if (name == null || name.length() == 0) {
            return Result.error("原材料名称不能为空");
        }
        if (name.length() > 20) {
            return Result.error("原材料名称过长");
        }
        if (num < 0 || num > 100000) {
            return Result.error("原材料数量范围有误");
        }
        if (gp < 0 || gp > 100000) {
            return Result.error("原材料保质期范围有误");
        }
        if (createTimeStr == null) {
            return Result.error("生产日期不能为空");
        }
        try {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            createTime = ft.parse(createTimeStr);
        } catch (ParseException e) {
            return Result.error("生产日期格式有误");
        }

        curMaterial.setName(name);
        curMaterial.setNum(num);
        curMaterial.setGuaranteePeriod(gp);
        curMaterial.setCreateTime(createTime);

        materialMapper.updateById(curMaterial);
        return Result.ok();
    }

    @Override
    public Result getAll() {
        List<Material> materials = materialMapper.selectList(null);
        return Result.ok(materials);
    }
}
